import sys

def litehtml_clean_attributes(html_content):
    """
    清理 HTML 标签中的多余属性，只保留 id= 和 class= 属性。
    :param html_content: 输入的HTML内容
    :return: 清理后的HTML内容
    """
    i = 0
    cleaned_content = []
    n = len(html_content)

    while i < n:
        # 检查是否进入标签
        if html_content[i] == '<':
            # 检查是否是结束标签
            if i + 1 < n and html_content[i + 1] == '/':
                # 结束标签直接保留
                cleaned_content.append(html_content[i])
                i += 1
            else:
                # 处理开始标签
                tag_start = i
                i += 1

                # 查找标签的结束符 '>'
                while i < n and html_content[i] != '>':
                    i += 1

                # 记录标签的结束位置
                if i < n and html_content[i] == '>':
                    i += 1  # 跳过 '>'
                    tag_end = i

                    # 提取标签内容
                    tag_content = html_content[tag_start:tag_end]

                    # 清理标签属性
                    cleaned_tag, removed_attributes = clean_tag_attributes(tag_content)

                    # 输出被删除的属性
                    if removed_attributes:
                        print(f"删除的属性: {removed_attributes}")

                    # 将清理后的标签添加到结果中
                    cleaned_content.append(cleaned_tag)
        else:
            # 非标签内容直接保留
            cleaned_content.append(html_content[i])
            i += 1

    return ''.join(cleaned_content)

def clean_tag_attributes(tag_content):
    """
    清理标签中的多余属性，只保留 id= 和 class= 属性。
    :param tag_content: 标签内容
    :return: 清理后的标签内容, 被删除的属性列表
    """
    i = 0
    cleaned_tag = []
    removed_attributes = []
    n = len(tag_content)

    # 添加标签的开头 '<'
    cleaned_tag.append(tag_content[i])
    i += 1

    # 提取标签的保留字
    while i < n and not tag_content[i].isspace() and tag_content[i] != '>':
        cleaned_tag.append(tag_content[i])
        i += 1

    # 提取属性
    while i < n and tag_content[i] != '>':
        # 跳过空白字符
        while i < n and tag_content[i].isspace():
            i += 1

        # 查找属性名
        attr_start = i
        while i < n and tag_content[i] != '=' and not tag_content[i].isspace():
            i += 1
        attr_name = tag_content[attr_start:i]

        # 查找属性值
        if i < n and tag_content[i] == '=':
            i += 1  # 跳过 '='
            # 查找属性值的起始引号
            while i < n and tag_content[i].isspace():
                i += 1
            if i < n and (tag_content[i] == '"' or tag_content[i] == "'"):
                quote_char = tag_content[i]
                i += 1  # 跳过引号
                attr_value_start = i
                # 查找属性值的结束引号
                while i < n and tag_content[i] != quote_char:
                    i += 1
                attr_value = tag_content[attr_value_start:i]
                i += 1  # 跳过引号

                # 只保留 id= 和 class= 属性
                if attr_name in ['id', 'class']:
                    cleaned_tag.append(f' {attr_name}="{attr_value}"')
                else:
                    removed_attributes.append(f'{attr_name}="{attr_value}"')
            else:
                # 如果没有引号，跳过整个属性值
                while i < n and not tag_content[i].isspace() and tag_content[i] != '>':
                    i += 1
        else:
            # 如果没有属性值，跳过
            while i < n and not tag_content[i].isspace() and tag_content[i] != '>':
                i += 1

    # 添加标签的结束符 '>'
    if i < n and tag_content[i] == '>':
        cleaned_tag.append(tag_content[i])

    return ''.join(cleaned_tag), removed_attributes

def main():
    """
    命令行工具的主函数。
    """
    # 检查命令行参数
    if len(sys.argv) != 2:
        print("用法: python litehtml.py <目标文件>")
        sys.exit(1)

    # 读取目标文件
    input_file = sys.argv[1]
    try:
        with open(input_file, 'r', encoding='utf-8') as file:
            html_content = file.read()
    except FileNotFoundError:
        print(f"错误: 文件 '{input_file}' 未找到。")
        sys.exit(1)
    except Exception as e:
        print(f"错误: 读取文件时发生错误 - {e}")
        sys.exit(1)

    # 清理 HTML 标签属性
    cleaned_html = litehtml_clean_attributes(html_content)

    # 输出清理后的 HTML 内容
    print("\n清理后的 HTML 内容：")
    print(cleaned_html)

if __name__ == "__main__":
    main()
